package com.fanshilz.flower2025.apps.admin.sysDictType.controller;

import cn.hutool.core.lang.Assert;
import com.fanshilz.flower2025.apps.admin.sysDictType.req.SysDictTypeSaveOrUpdateReq;
import com.fanshilz.flower2025.apps.admin.sysDictType.req.SysDictTypeTableReq;
import com.fanshilz.flower2025.apps.admin.sysDictType.resp.SysDictTypeTableDetailResp;
import com.fanshilz.flower2025.business.sys.sysDictType.entity.SysDictType;
import com.fanshilz.flower2025.business.sys.sysDictType.service.ISysDictTypeService;
import com.fanshilz.flower2025.framework.exception.CodeException;
import com.fanshilz.flower2025.framework.resp.Resp;
import com.fanshilz.flower2025.framework.resp.TableResp;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import io.github.linpeilie.Converter;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/admin/dictType")
@RestController
public class SysDictTypeController {

    @Resource
    private Converter converter;
    @Resource
    private ISysDictTypeService sysDictTypeService;

    @PostMapping("/remove/{dictTypeId}")
    public Resp<Void> remove(@PathVariable Long dictTypeId) {
        boolean b = sysDictTypeService.updateById(SysDictType.builder()
                .dictTypeId(dictTypeId)
                .delFlag(true)
                .build());
        Assert.isTrue(b, CodeException::new);
        return Resp.success();
    }

    @PostMapping("/saveOrUpdate")
    public Resp<Void> saveOrUpdate(@RequestBody @Valid SysDictTypeSaveOrUpdateReq req) {
        SysDictType sc = converter.convert(req, SysDictType.class);
        boolean b = sysDictTypeService.saveOrUpdate(sc);
        Assert.isTrue(b, CodeException::new);
        return Resp.success();
    }

    @PostMapping("/table")
    public Resp<TableResp<SysDictTypeTableDetailResp>> table(@RequestBody @Valid SysDictTypeTableReq req) {
        Page<SysDictTypeTableDetailResp> page = sysDictTypeService.pageAs(Page.of(req.getCurrent(), req.getPageSize(), req.getTotal()),
                QueryWrapper.create(), SysDictTypeTableDetailResp.class);
        return Resp.success(new TableResp<>(page.getRecords(), page.getTotalRow()));
    }

}
